﻿package Leophy.controls.Lists 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import Leophy.controls.MotionClip.EasyMotionMovieClip;
	import Leophy.controls.MotionClip.MontionFuns;
	import Leophy.controls.MyDisplayObjectContainer;
	import Leophy.Interfaces.IControls.IScroll.IScorllEnable;
	import Leophy.Interfaces.IControls.IScroll.IScrollUI;
	import Leophy.Interfaces.IControls.List.IListItem;
	import Leophy.LeophyVersion;
	
	/**
	 * ...
	 * @author TOPHY
	 */
	public class List extends MyDisplayObjectContainer 
	{
		////====================================  事件
		
		public static var M_SELECTED_CHANGED:String = "onSelectedItemsChanged";
		
		////====================================  变量
		private var _items:Array;
		private var _itemHolder:MovieClip;

		//private var _itemXSpace:Number = 0;
		private var _itemYSpace:Number = 2;
		
		private var _lastItem:IListItem;
		private var _selectedItems:Array;
		private var _multSelecte:Boolean=true;
	
		////====================================  属性
		public function get SelectedItems():Array {
			var selectItem:Array = _selectedItems.filter(defaultFilter);
			return selectItem;
		}
		public function get MultSelect():Boolean {
			return _multSelecte;
		}
		public function set MultSelect(value:Boolean):void {
			this._multSelecte = value;
		}
		public function get ItemYSpace():Number {
			return this._itemYSpace;
		}
		public function set ItemYSpace(value:Number):void {
			_itemYSpace = value;
			fixShowItemPosition();
		}
		////====================================  函数
		public function List() 
		{
			LeophyVersion.version();
			super();
			_items = new Array();
			_itemHolder = super.Container;
			_selectedItems = new Array();
			_itemHolder.mouseEnabled = false;
		
		}
		
		
		/**
		 * 添加一个Item
		 * @param	Item IListItem类型的节点
		 * @return
		 */
		public function AddItem(Item:IListItem):uint {
			var ls:uint = _items.push(Item);
			Item.bindList(this);
			this._itemHolder.addChild(DisplayObject(Item));
			if (_items.length > 1) {
				//var proitem:IListItem=IListItem(_items[_items.length-1])
				Item.setItemY(_lastItem.itemHeight() + _lastItem.itemY()+_itemYSpace);
			}
			_lastItem = Item;
			UpdataScrolls();
			return ls;
		}
		/**
		 * 删除一个节点
		 * @param	Item IListItem类型的节点
		 * @return
		 */
		public function RemoveItem(Item:IListItem):IListItem {
			var index:uint = _items.indexOf(Item);
			
			_items.splice(index, 1);
			this.DelSelectedItem(Item, true);
			
			this._itemHolder.removeChild(ListItem(Item));
			fixShowItemPosition();
			UpdataScrolls();
			return Item;
		}
		/**
		 * 删除所有节点
		 */
		public function Clear():void {
			for (var i:uint = 0; i < _items.length; i++) {
				try {
						
						this._itemHolder.removeChild(_items[i]);
						var index:uint = _items.indexOf(_items[i]);
						_items.splice(index, 1);
						
						if (_items[i] != null) {
							if (IListItem(_items[i]).getSelection()) {
								this.DelSelectedItem(_items[i],true);
							}
						}
						i--;
					
				}catch (ev:*) {
					trace(_items[i],"maybe has been removed!\r",ev.toString());
				}
			}
			UpdataScrolls();
			_lastItem = null;
		}
		/**
		 * 设置过滤器
		 * @param	filterFun　原型：function collback(item:IListItem,index:uint,itemArray:Array):Boolean,返回false则过滤
		 * @param	thisObj
		 */
		public function Filter(filterFun:Function=null, thisObj:*=null) {
			if (filterFun == null) {
				filterFun = this.defaultFilter;
			}
			if (thisObj == null)
			{
				thisObj = this;
			}
			
			for (var i:uint = 0; i < _items.length; i++) {
				if (true == filterFun.call(thisObj, _items[i], i, _items) ) {
					
					_items[i].setVisible(true);
				}else {
					_items[i].setVisible(false);
				}
			}
			fixShowItemPosition();
			VValue = 0;
		}
		
		
		/**
		 * [内部使用]
		 * @param	Item　添加选中节点至选中列表
		 */
		public function AddSelectedItem(Item:IListItem = null) {
			if (Item != null) {
				if (_selectedItems.indexOf(Item) == -1) {
					if(_multSelecte){
							_selectedItems.push(Item);
						
					}else {
						for (var i:uint = 0; i < _selectedItems.length; i++) {
							IListItem(_selectedItems[i]).setSelection(false);
						}
						_selectedItems.push(Item);
					}
					this.dispatchEvent(new Event(M_SELECTED_CHANGED));
				}
			}
		}
		/**
		 * [内部使用]
		 * @param	Item　删除选中节点列表项
		 */
		public function DelSelectedItem(Item:IListItem = null,forRemoveNode:Boolean=false) {
			if (Item != null) {
				if (index != -1) {
					if (_multSelecte) {
						var index:int = _selectedItems.indexOf(Item);
							_selectedItems.splice(index, 1);
					}else {
						_selectedItems = new Array();
					}
					if(!forRemoveNode){
						this.dispatchEvent(new Event(M_SELECTED_CHANGED));
					}
				}
			}
		}
		////====================================  item
		private function defaultFilter(...arg:*):Boolean {
			return true;
		}
		private function fixShowItemPosition() {
			var lastVisibleItem:IListItem;
			if (_items.length <= 0) {
				_lastItem = null;
			}
			for (var i:uint = 0; i < _items.length; i++) {
				var tempi:IListItem = IListItem(_items[i]);
				if (tempi.getVisible()) {
					
					if (lastVisibleItem == null) {
						tempi.setItemX(0);
						tempi.setItemY(0);
					}else {
						tempi.setItemX(0);
						tempi.setItemY(lastVisibleItem.itemY() + lastVisibleItem.itemHeight() + _itemYSpace);
					}
					lastVisibleItem = tempi;
				}else {
					tempi.setItemX(0);
					tempi.setItemY(0);
				}
				_lastItem = tempi;
			}
			
		}
		////====================================  scroll
		/**/
		
		override protected function setMaxVScrollValue() {
			if (_lastItem == null) {
				_maxScrollV = 0;
				return;
			}
			var yv:Number = _lastItem.itemY();
			var hv:Number = _lastItem.itemHeight()+2;
			_maxScrollV = yv + hv - this.ShowRectangle.height;
			
		}
		override protected function setMaxHScrollValue() {
			if (_lastItem == null) {
				_maxScrollH = 0;
				return;
			}
			var yh:Number = _lastItem.itemX();
			var hh:Number = _lastItem.itemWidth()+2;
			_maxScrollH = yh + hh - this.ShowRectangle.width;
			
		}
	}
	
}